package com.sun.lib_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sun.lib.STextView;
import com.sun.lib_tag.TagAdapter;
import com.sun.lib_tag.TagFlowGroup;
import com.sun.lib_tag.TagFlowLayout;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private TagFlowLayout tagFlowLayout;
    private TagFlowLayout tagFlowLayout2;

    private boolean isEdit = false;
    private STextView editView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // tagFlowLayout = findViewById(R.id.tagLayout);
        tagFlowLayout2 = findViewById(R.id.tagLayout2);

       /* tagFlowLayout.setAdapter(new TagAdapter<String>() {
            @Override
            public View getView(TagFlowGroup parent, int position, String string) {
                TextView titleView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tag, parent, false);
                titleView.setText(string);
                return titleView;
            }
        });
        tagFlowLayout.setOnTagSelectListener(new TagFlowLayout.OnTagSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPos) {
                System.out.println("选中项:");
                for (Integer pos : selectPos) System.out.print(pos + "\n");
            }
        });

        tagFlowLayout.getAdapter().setTags("哈哈哈哈", "语文", "数据", "斗破苍穹", "星辰变", "的地方是德国");*/

        final MyAdapter tagAdapter = new MyAdapter();
        tagFlowLayout2.setAdapter(tagAdapter);

        tagAdapter.setTags("哈哈哈哈", "语文", "数据", "斗破苍穹", "星辰变", "凡人修仙传", "一念之间");
        editView = findViewById(R.id.editView);
        editView.setText(isEdit ? "完成" : "编辑");
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEdit = !isEdit;
                tagAdapter.showDel(isEdit);
                editView.setText(isEdit ? "完成" : "编辑");
            }
        });

    }

    private class MyAdapter extends TagAdapter<String>{
        private boolean showDel = false;

        @Override
        public View getView(TagFlowGroup parent, final int position, String s) {
            View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tag_editable, parent, false);
            STextView textView = root.findViewById(R.id.titleView);
            ImageView delView = root.findViewById(R.id.delView);
            delView.setVisibility(showDel ? View.VISIBLE : View.GONE);
            textView.setText(s);
            delView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    del(position);
                }
            });
            return root;
        }

        public void del(int pos) {
            getDataList().remove(pos);
            notifyDataChanged();
        }

        public void showDel(boolean status) {
            this.showDel = status;
            notifyDataChanged();
        }
    }
}
